#!/bin/bash

##############################################
# 盲盒抽奖功能更新部署脚本
# 版本: v1.1.0
# 日期: 2025-10-30
##############################################

echo "=========================================="
echo "  盲盒抽奖功能更新部署"
echo "  版本: v1.1.0"
echo "=========================================="
echo ""

# 设置颜色
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 项目路径
PROJECT_DIR="/www/wwwroot/sm_shop-tp6"
TP8_DIR="$PROJECT_DIR/sm_shop_tp8"

# 检查是否在正确的目录
if [ ! -d "$PROJECT_DIR" ]; then
    echo -e "${RED}错误: 项目目录不存在: $PROJECT_DIR${NC}"
    echo "请修改脚本中的 PROJECT_DIR 变量"
    exit 1
fi

cd $PROJECT_DIR

echo -e "${YELLOW}步骤 1/5: 检查环境...${NC}"
echo "项目路径: $PROJECT_DIR"
echo "ThinkPHP路径: $TP8_DIR"
echo ""

# 检查ThinkPHP目录
if [ ! -d "$TP8_DIR" ]; then
    echo -e "${RED}错误: ThinkPHP目录不存在: $TP8_DIR${NC}"
    exit 1
fi

# 检查迁移文件
MIGRATION_FILE="$TP8_DIR/database/migrations/20251030000001_add_draw_time_to_order_blind_box.php"
if [ ! -f "$MIGRATION_FILE" ]; then
    echo -e "${RED}错误: 迁移文件不存在${NC}"
    echo "文件: $MIGRATION_FILE"
    exit 1
fi

echo -e "${GREEN}✓ 环境检查通过${NC}"
echo ""

# 询问用户是否继续
echo -e "${YELLOW}步骤 2/5: 确认更新${NC}"
echo "即将执行以下操作："
echo "  1. 为 sh_order_blind_box 表添加 draw_time 字段"
echo "  2. 代码已自动更新（控制器和模型）"
echo ""
read -p "是否继续？(y/n): " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}已取消更新${NC}"
    exit 0
fi

# 备份提示
echo ""
echo -e "${YELLOW}步骤 3/5: 数据库备份（推荐）${NC}"
echo "强烈建议在执行迁移前备份数据库！"
echo ""
read -p "是否已完成数据库备份？(y/n): " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}请先备份数据库，然后重新运行此脚本${NC}"
    echo ""
    echo "备份命令示例："
    echo "mysqldump -u your_user -p your_database > backup_$(date +%Y%m%d_%H%M%S).sql"
    exit 0
fi

# 执行迁移
echo ""
echo -e "${YELLOW}步骤 4/5: 执行数据库迁移...${NC}"
cd $TP8_DIR

if command -v php &> /dev/null; then
    echo "执行迁移命令: php think migrate:run"
    php think migrate:run
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ 数据库迁移成功${NC}"
    else
        echo -e "${RED}✗ 数据库迁移失败${NC}"
        echo ""
        echo "请尝试手动执行SQL文件："
        echo "mysql -u your_user -p your_database < $TP8_DIR/sql/20251030_add_draw_time_to_order_blind_box.sql"
        exit 1
    fi
else
    echo -e "${RED}错误: 未找到 php 命令${NC}"
    echo "请手动执行SQL文件："
    echo "mysql -u your_user -p your_database < $TP8_DIR/sql/20251030_add_draw_time_to_order_blind_box.sql"
    exit 1
fi

# 验证安装
echo ""
echo -e "${YELLOW}步骤 5/5: 验证安装...${NC}"
echo "请手动验证数据库表结构："
echo ""
echo "执行以下SQL语句："
echo "  DESC sh_order_blind_box;"
echo ""
echo "期望看到 draw_time 字段（DATETIME类型）"
echo ""

# 完成
echo ""
echo -e "${GREEN}=========================================="
echo "  更新完成！"
echo "==========================================${NC}"
echo ""
echo "接下来的步骤："
echo ""
echo "1. 验证数据库表结构"
echo "   DESC sh_order_blind_box;"
echo ""
echo "2. 测试抽奖接口"
echo "   POST /api/blindbox.draw"
echo ""
echo "3. 查看文档"
echo "   - 快速指南: $PROJECT_DIR/BLINDBOX_DRAW_QUICKSTART.md"
echo "   - 完整文档: $PROJECT_DIR/BLINDBOX_DRAW_API.md"
echo "   - 更新日志: $PROJECT_DIR/CHANGELOG_BLINDBOX_DRAW.md"
echo ""
echo -e "${YELLOW}如有问题，请参考文档或联系技术支持。${NC}"
echo ""
